<template>
	<view class="active_box box">
		<view class="top">
			<view class="box-bg">
				<uni-nav-bar>
					<block slot="left">
						<view class="city" @click="gotoChoseCity" style="width: 140rpx;">
							<view class="uni-nav-bar-text">
								{{ city||'定位中' }}
							</view>
							<uni-icons type="arrowdown" color="#000" size="18" />
						</view>
					</block>
					<view class="input-view">
						<uni-icons class="input-uni-icon" type="search" size="18" color="#999" />
						<input confirm-type="search" class="nav-bar-input" type="text" placeholder="搜索"
							@confirm="serchActiveByKeyWord" v-model="searchVal" />
					</view>
				</uni-nav-bar>
			</view>
		</view>
		<view class="active_box_container">
			<view class="active_box_container_nine_box">
				<!-- <view :class="['active_box_container_nine_box_item' + (index+1), 'chose'+ (index+1)]"
					class="active_box_container_nine_box_item" v-for='(item,index) in activeType'
					@click="choseType(item,index)">
					{{item.name}}
				</view> -->
				<view
					:class="['active_box_container_nine_box_item' + (index+1), index == choseId ? 'chose' + (index+1) : '']"
					class="active_box_container_nine_box_item" v-for='(item,index) in activeType' :key="index"
					@click="choseType(item,index)">
					{{item.name}}
				</view>

			</view>
			<view class="" v-if="activeData&&activeData!=null" @click="gotoActiveDetail(item,index)"
				v-for="(item,index) in activeData" style="margin-bottom: 40rpx;" :key="index">
				<view class="">
					<img :src="item.image" alt=""
						style="width: 670rpx;height: 244rpx;background: #D8D8D8;border-radius: 24rpx;" />
				</view>
				<view class="active_box_title">
					{{item.title || '未知'}}
				</view>
				<view class="active_box_time_item">
					<view class="active_box_time">
						<image src="../../static/home/time.png" style="width: 26rpx;height: 26rpx;"></image>
					</view>
					<view class="active_box_time_item_text">
						{{times[index]}}
					</view>
				</view>
				<view class="active_box_time_item_desc">
					{{item.desc||'暂无'}}
					<!-- {{latitude}}--{{longitude}} -->
				</view>
				<view class="">
					<image src="../../static/activity/postion.png" mode="" style="width: 24rpx;height: 24rpx;"></image>
					<text>{{item.address || '未知'}}</text>
				</view>
				<view class="" style="margin-top: 4rpx;">
					<image src="../../static/activity/flag.png" mode="" style="width: 24rpx;height: 24rpx;"></image>
					<text>距离您 <text style="color: #FA5241;line-height: 30rpx;">{{distanceArr[index]||'未知'}}</text>
						公里</text>
				</view>
			</view>
			<view class="" v-if='activeData==null' class="nonemore">
				暂无数据
			</view>
			<view class="" v-if='nomore' class="nonemore" style="padding-bottom: 20rpx;">
				没有更多了
			</view>
		</view>
	</view>
</template>

<script>
	import distance from '@turf/distance';
	import {
		timestampToDateTime
	} from '../../utils/time.js';
	import {
		getActive,
		activityType
	} from "../../api/request.js"
	import {
		getLocationInfo
	} from "../../utils/location.js"
	export default {
		data() {
			return {
				city: '定位中',
				cityCode: 0,
				activeData: [],
				times: [],
				activeType: [],
				distanceArr: [],
				longitude: 0,
				latitude: 0,
				isShow: false,
				total: 0,
				pageNo: 1,
				pageSize: 10,
				searchVal: '',
				nomore: false,
				isChoseCity: false,
				chose1: false,
				chose2: false,
				chose3: false,
				chose4: false,
				chose5: false,
				chose6: false,
				choseId: null,
				flag: true
			};
		},
		onShow() {
			const that = this
			if (this.isChoseCity) {
				that.pageNo = 1
				this.isChoseCity = false
				uni.getStorage({
					key: 'choseCtiy',
					success: async function(res) {
						let result = await getActive({
							pageNo: that.pageNo,
							keyWord: that.searchVal || '',
							cityCode: res.data.code || ''
						})
						that.activeData = result.records
						that.total = result.total
						that.isShow = true
					},
					fail(err) {
						uni.getStorage({
							key: 'firstCtiy',
							success: async function(res) {
								let result = await getActive({
									pageNo: that.pageNo,
									keyWord: that.searchVal || '',
									cityCode: res.data || ''
								})
								that.activeData = result.records
								that.total = result.total
								that.isShow = true
							},
							fail(err) {

								console.log(err, 'firstCtiy kjsjsgasgdjagdsgd');
							}
						});
						console.log(err, 'choseCtiy kjsjsgasgdjagdsgd');
					}
				});
			}
			uni.getStorage({
				key: 'choseCtiy',
				success: function(res) {
					console.log(res.data);
					if (res.data.name.length > 2) {
						that.city = res.data.name
					} else {
						that.city = res.data.name.replace(/(.{1})/, '$1 ')
					}
					that.cityCode = res.data.code
				}
			});
			uni.showTabBar()
		},
		async onReachBottom(e) {
			console.log(this.total / 10, this.pageNo);
			if (this.pageNo <= this.total / 10 && this.total > 10) {
				const that = this
				this.pageNo += 1
				uni.getStorage({
					key: 'choseCtiy',
					success: async function(res) {
						let result = await getActive({
							pageNo: that.pageNo,
							keyWord: that.searchVal || '',
							cityCode: res.data.code || ''
						})
						if (result.records) {
							result.records.map((item) => {
								that.activeData.push(item)
								setTimeout(() => {
									that.times.push(timestampToDateTime(item.startTime))
								}, 1000)
								console.log(item.latitude, that.latitude, item.longitude, that
									.longitude);
								if (item.latitude && that.latitude && item.longitude && that
									.longitude) {
									let from = [item.longitude, item.latitude];
									let to = [that.longitude, that.latitude];

									let options = {
										units: 'kilometers'
									};
									let dis = distance(from, to, options).toFixed(2);
									that.distanceArr.push(dis)
									console.log(dis, 'dis');
								}
							})
						}
						that.total = result.total
						that.isShow = true
						console.log(that.activeData, 'choseCtiy that.activeData');
					},
					fail(err) {
						uni.getStorage({
							key: "firstCtiy",
							async success(res) {
								let result = await getActive({
									pageNo: that.pageNo,
									keyWord: that.searchVal || ''
								})
								if (result.records) {
									result.records.map((item) => {
										that.activeData.push(item)
										that.times.push(timestampToDateTime(item
											.startTime))
										if (item.latitude && that.latitude && item
											.longitude && that.longitude) {
											let from = [item.longitude, item.latitude];
											let to = [that.longitude, that.latitude];

											let options = {
												units: 'kilometers'
											};
											let dis = distance(from, to, options).toFixed(
												2);
											that.distanceArr.push(dis)
											console.log(dis, 'dis');
										}
									})
								}
								that.total = result.total
								that.isShow = true
								console.log(that.activeData, 'choseCtiy fail  that.activeData');
							},
							fail() {

							}
						})
						console.log(err, 'kjsjsgasgdjagdsgd');
					}
				});
			} else {
				console.log('ksahkjhsdljhdalkj');
				this.nomore = true
				// uni.showToast({
				// 	icon: "none",
				// 	title: "已加载全部了"
				// })
				// console.log('加载不存在的数据');
			}

		},
		onLoad(e) {
			let that = this
			if (e.keyWords && e.keyWords != 'null') {
				const that = this
				this.searchVal = e.keyWords
				uni.getStorage({
					key: 'choseCtiy',
					success: async function(res) {
						let result = await getActive({
							pageNo: that.pageNo,
							keyWord: e.keyWords || '',
							cityCode: res.data.code || ''
						})
						that.activeData = result.records
						that.total = result.total
						that.isShow = true
						console.log(res, 'kjsjsgasgdjagdsgd', e.keyWords, 'e.keyWords');
					},
					fail(err) {
						uni.getStorage({
							key: 'firstCtiy',
							success: async function(res) {
								let result = await getActive({
									pageNo: that.pageNo,
									keyWord: e.keyWords || ''
								})
								that.activeData = result.records
								that.total = result.total
								that.isShow = true
								console.log(res, 'kjsjsgasgdjagdsgd', e.keyWords, 'e.keyWords');
							},
							fail(err) {

								console.log(err, 'kjsjsgasgdjagdsgd');
							}
						});
						console.log(err, 'kjsjsgasgdjagdsgd');
					}
				});
			} else {

				console.log(distance, 'distance');
				setTimeout(() => {
					this.getData()
					this.isShow = true
				}, 2000)
			}

			uni.getStorage({
				key: 'choseCtiy',
				success: async function(res) {
					if (res.data.name.length > 2) {
						that.city = res.data.name
					} else {
						that.city = res.data.name.replace(/(.{1})/, '$1 ')
					}
					console.log(res.data, 'choseCtiy res.data');
				},
				fail(err) {
					uni.getStorage({
						key: 'firstCtiyName',
						success: async function(res) {
							if (res.data.name.length > 2) {
								that.city = res.data.name
							} else {
								that.city = res.data.name.replace(/(.{1})/, '$1 ')
							}
						},
						fail(err) {}
					});
				}
			});
			this.getActivityType()
			this.getCurrentLocation()
		},
		methods: {
			async choseType(item, index) {
				if (index != this.choseId) {
					const that = this
					this.choseId = index
					that.pageNo = 1
					uni.getStorage({
						key: 'choseCtiy',
						success: async function(res) {
							that.distanceArr = []
							let result = await getActive({
								pageNo: that.pageNo,
								typeId: item.id || '',
								cityCode: res.data.code || '',
								keyWord: that.keyWords || '',
							})
							that.activeData = result.records
							that.total = result.total
							that.isShow = true
							if (result.records) {
								that.activeData = result.records
								that.activeData.map((item) => {
									that.times.push(timestampToDateTime(item.startTime))
									console.log(item.latitude, that.latitude, item.longitude, that
										.longitude);
									if (item.latitude && that.latitude && item.longitude && that
										.longitude) {
										let from = [item.longitude, item.latitude];
										let to = [that.longitude, that.latitude];

										let options = {
											units: 'kilometers'
										};
										let dis = distance(from, to, options).toFixed(2);
										that.distanceArr.push(dis)
										console.log(dis, 'dis');
									}
								})
							}
						},
						fail(err) {
							uni.getStorage({
								key: 'firstCtiy',
								success: async function(res) {
									let result = await getActive({
										pageNo: that.pageNo,
										keyWord: that.keyWords || '',
										typeId: item.id || '',
									})
									that.activeData = result.records
									that.total = result.total
									that.isShow = true
									console.log(res);
								},
								fail(err) {

									console.log(err, 'kjsjsgasgdjagdsgd');
								}
							});
							console.log(err, 'kjsjsgasgdjagdsgd');
						}
					});
				} else {
					const that = this
					this.choseId = null
					that.pageNo = 1
					uni.getStorage({
						key: 'choseCtiy',
						success: async function(res) {
							that.distanceArr = []
							let result = await getActive({
								pageNo: that.pageNo,
								cityCode: res.data.code || '',
								keyWord: that.keyWords || '',
							})
							that.activeData = result.records
							that.total = result.total
							that.isShow = true
							if (result.records) {
								that.activeData = result.records
								that.activeData.map((item) => {
									that.times.push(timestampToDateTime(item.startTime))
									console.log(item.latitude, that.latitude, item.longitude, that
										.longitude);
									if (item.latitude && that.latitude && item.longitude && that
										.longitude) {
										let from = [item.longitude, item.latitude];
										let to = [that.longitude, that.latitude];

										let options = {
											units: 'kilometers'
										};
										let dis = distance(from, to, options).toFixed(2);
										that.distanceArr.push(dis)
										console.log(dis, 'dis');
									}
								})
							}
						},
						fail(err) {
							uni.getStorage({
								key: 'firstCtiy',
								success: async function(res) {
									let result = await getActive({
										pageNo: that.pageNo,
										keyWord: that.keyWords || '',
									})
									that.activeData = result.records
									that.total = result.total
									that.isShow = true
									console.log(res);
								},
								fail(err) {

									console.log(err, 'kjsjsgasgdjagdsgd');
								}
							});
							console.log(err, 'kjsjsgasgdjagdsgd');
						}
					});
				}
			},
			async serchActiveByKeyWord(e) {
				const that = this
				uni.getStorage({
					key: 'choseCtiy',
					success: async function(res) {
						let result = await getActive({
							pageNo: 1,
							keyWord: e.target.value || '',
							cityCode: res.data.code || ''
						})
						that.activeData = result.records
						that.total = result.total
						console.log('serchActiveByKeyWord');
					},
					fail() {
						uni.getStorage({
							key: "firstCtiy",
							success: async function(res) {
								let result = await getActive({
									pageNo: 1,
									keyWord: e.target.value || '',
								})
								that.activeData = result.records
								that.total = result.total
								console.log('serchActiveByKeyWord');
							},
							fail() {
								uni.showToast({
									icon: "none",
									title: "暂时无法获取您的城市信息"
								})
							}
						})
					}
				});

				console.log(e.target.value, 'serchActiveByKeyWord');
			},
			gotoChoseCity() {
				this.isChoseCity = true
				uni.navigateTo({
					url: "../../subPackage/choseCity/choseCity"
				})
			},
			async paginationChose(e) {
				console.log(e, 'paginationChose');
				let res = await getActive({
					pageNo: e.current,
					keyWord: e.target.value || '',
					cityCode: res.data.code || ''
				})
				console.log('paginationChose');
				this.activeData = res.records
				this.total = res.total
			},
			getCurrentLocation() {
				const that = this
				uni.getLocation({
					type: 'wgs84',
					geocode: true,
					success: function(res) {
						that.longitude = res.longitude
						that.latitude = res.latitude
						console.log('地址', res);
						// that.city=res.
						getLocationInfo(res.longitude, res.latitude)
					},
					fail(e) {
						console.log(e);
					}
				});

			},
			async getActivityType() {
				let res = await activityType()
				this.activeType = res
				console.log(res, 'activityType');
			},
			async getData() {
				const that = this
				uni.getStorage({
					key: 'choseCtiy',
					success: async function(res) {
						let result = await getActive({
							pageNo: that.pageNo,
							cityCode: res.data.code || ''
						})
						that.activeData = result.records
						that.total = result.total
						console.log('serchActiveByKeyWord');
						console.log('getData');
						that.total = result.total
						if (result.records) {
							that.activeData = result.records
							that.activeData.map((item) => {
								that.times.push(timestampToDateTime(item.startTime))
								console.log(item.longitude, item.latitude, that.longitude, that
									.latitude);
								if (item.latitude && that.latitude && item.longitude && that
									.longitude) {
									let from = [item.longitude, item.latitude];
									let to = [that.longitude, that.latitude];
									let options = {
										units: 'kilometers'
									};
									let dis = distance(from, to, options).toFixed(2);
									that.distanceArr.push(dis)
									console.log(dis, 'dis');
								}
							})
						}
					},
					fail: async function(err) {
						uni.getStorage({
							key: 'firstCtiy',
							success: async function(res) {
								let result = await getActive({
									pageNo: that.pageNo,
									keyWord: that.searchVal || '',
								})
								that.activeData = result.records
								that.total = result.total
								console.log('serchActiveByKeyWord');
								console.log('getData');
								that.total = result.total
								if (result.records) {
									that.activeData = result.records
									that.activeData.map((item) => {
										that.times.push(timestampToDateTime(item
											.startTime))
										console.log(item.longitude, item.latitude, that
											.longitude, that
											.latitude);
										if (item.latitude && that.latitude && item
											.longitude && that
											.longitude) {
											let from = [item.longitude, item.latitude];
											let to = [that.longitude, that.latitude];
											let options = {
												units: 'kilometers'
											};
											let dis = distance(from, to, options)
												.toFixed(2);
											that.distanceArr.push(dis)
											console.log(dis, 'dis');
										}
									})
								}
							},
							fail(err) {

								console.log(err, 'firstCtiy kjsjsgasgdjagdsgd');
							}
						});

					}
				});


			},
			gotoActiveDetail(item, index) {
				console.log(item, 'item');
				const {
					id
				} = item
				uni.navigateTo({
					url: "/pages/eventDetails/eventDetails?id=" + id
				})
			},
		}
	}
</script>

<style lang="scss">
	$nav-height: 30px;

	.chose1 {
		background-image: url("../../static/activity/1-1.png") !important;
		color: white !important;
	}

	.chose2 {
		background-image: url("../../static/activity/2-2.png") !important;
		color: white !important;
	}

	.chose3 {
		background-image: url("../../static/activity/3-3.png") !important;
		color: white !important;
	}

	.chose4 {
		background-image: url("../../static/activity/4-4.png") !important;
		color: white !important;
	}

	.chose5 {
		background-image: url("../../static/activity/5-5.png") !important;
		color: white !important;
	}

	.chose6 {
		background-image: url("../../static/activity/6-6.png") !important;
		color: white !important;
	}

	.uni-navbar__header-btns-left {
		width: 27% !important;
	}


	.uni-navbar__header-container {
		background-color: #FFF1F0 !important;
	}

	.nonemore {
		width: 100%;
		height: 40rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 28rpx;
		color: #A3A3A3;
		line-height: 40rpx;
		text-align: center;
		font-style: normal;
		display: inline-block;
		margin-top: 40rpx;
	}

	.uni-nav-bar-text {
		font-family: PingFangSC, PingFang SC;
		font-weight: 600;
		font-size: 36rpx;
		color: #000000;
		line-height: 50rpx;
		text-align: left;
		font-style: normal;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		max-width: 135rpx;
		min-width: 114rpx;
	}

	.top {
		min-height: 40vh;
		position: absolute;
		z-index: 10000;
		top: 94rpx;
		left: 42rpx;

		.big_img {
			position: absolute;
		}

		.top_container {
			margin-top: 66rpx;
			width: 100%;
			display: flex;
			position: absolute;
			z-index: 1000;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;

			.index_address {
				width: 72rpx;
				height: 50rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 36rpx;
				color: #000000;
				line-height: 50rpx;
				text-align: left;
				font-style: normal;
				margin-left: 40rpx;
				width: 14%;
				margin-top: 24rpx;
			}
		}
	}

	.box-bg {
		padding: 5px 0;
		// margin-top: 94rpx; 
	}

	.uniui-search {
		color: #FA301B !important;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		width: 130rpx;
	}

	.input-view {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		// width: 500rpx;
		flex: 1;
		height: $nav-height;
		border-radius: 15px;
		// padding: 0 15px;
		flex-wrap: nowrap;
		height: 100%;
		align-items: center;
		// margin: 7px 0;
		// line-height: $nav-height;
	}

	.input-uni-icon {
		line-height: $nav-height;
	}

	.nav-bar-input {
		height: $nav-height;
		line-height: $nav-height;
		/* #ifdef APP-PLUS-NVUE */
		width: 370rpx;
		/* #endif */
		padding: 0 5px;
		font-size: 28rpx;
	}

	.uni-navbar--border {
		background-color: transparent !important;
		border: 0rpx !important;
	}

	.uni-navbar__header {
		background-color: transparent !important;
	}

	.uni-navbar__header-container {
		background-color: white;
	}

	.uni-navbar__header-container {
		width: 300rpx;
		height: 72rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		margin-left: 20rpx;
	}

	.uni-navbar__header-btns {
		// width: 72rpx;
		// height: 50rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 600;
		font-size: 36rpx;
		color: #FFFFFF;
		line-height: 50rpx;
		text-align: left;
		font-style: normal;
		width: 90rpx;
	}

	.uni-navbar__header-btns:last-child {
		display: none;
	}

	.uni-navbar__header-btns-right {
		display: none;
	}

	.top {
		min-height: 208rpx;

		.big_img {
			position: absolute;
		}

		.top_container {
			margin-top: 66rpx;
			width: 100%;
			display: flex;
			position: absolute;
			z-index: 1000;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;

			.index_address {
				width: 72rpx;
				height: 50rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 36rpx;
				color: #000000;
				line-height: 50rpx;
				text-align: left;
				font-style: normal;
				margin-left: 40rpx;
				display: flex;
				width: 14%;
				margin-top: 24rpx;
			}
		}


	}

	.active_box_title {
		width: 360rpx;
		height: 42rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 500;
		font-size: 30rpx;
		color: #1A0200;
		line-height: 42rpx;
		text-align: left;
		margin-top: 24rpx;
		margin-bottom: 12rpx;
		font-style: normal;
	}

	.active_box_time_item {
		width: 320rpx;
		height: 48rpx;
		background: rgba(250, 48, 27, .1);
		border-radius: 16rpx;
		display: flex;
	}

	.active_box_time {
		padding: 10rpx;
	}

	.active_box_time image {}

	.active_box_time_item_text {
		width: 218rpx;
		height: 30rpx;
		font-family: DINAlternate, DINAlternate;
		font-weight: bold;
		font-size: 26rpx;
		color: #FA2C17;
		line-height: 30rpx;
		text-align: left;
		font-style: normal;
		white-space: nowrap;
		line-height: 48rpx;
	}

	.active_box_time_item_desc {
		width: 646rpx;
		height: 72rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 26rpx;
		color: #9f9594;
		line-height: 36rpx;
		text-align: left;
		font-style: normal;
		margin-top: 24rpx;
		overflow: hidden;
	}

	.active_box_container {
		width: 90%;
		margin: auto;
		margin-top: 208rpx;
		z-index: 1000000;
		position: absolute;
		left: 0;
		right: 0;

		.active_box_container_nine_box {

			.active_box_container_nine_box_item {
				// width: 122rpx;
				height: 88rpx;
				float: left;
				line-height: 50px;
				padding-left: 88rpx;
				background-repeat: no-repeat;
				background-size: 100% 100%;
				margin-bottom: 20rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #000000;
				line-height: 88rpx;
				font-style: normal;
				margin-right: 18rpx;
			}

			.active_box_container_nine_box_item1 {
				background-image: url("../../static/activity/1.png");

			}

			.active_box_container_nine_box_item2 {
				background-image: url("../../static/activity/2.png");

			}

			.active_box_container_nine_box_item3 {
				background-image: url("../../static/activity/3.png");

			}

			.active_box_container_nine_box_item4 {
				background-image: url("../../static/activity/4.png");

			}

			.active_box_container_nine_box_item5 {
				background-image: url("../../static/activity/5.png");

			}

			.active_box_container_nine_box_item6 {
				background-image: url("../../static/activity/6.png");

			}
		}
	}
</style>